<template>
<div>
  <el-table
    :data="admins"
    style="width: 100%"
    :row-class-name="tableRowClassName">
    <el-table-column
      prop="u_id"
      label="用户编号"
      width="80">
    </el-table-column>
    <el-table-column
      prop="u_nickname"
      label="用户名字"
      width="100">
    </el-table-column>
    <el-table-column
      prop="u_nickname"
      label="角色昵称"
      width="100">
    </el-table-column>
    <el-table-column
      prop="u_class"
      label="账户等级"
      width="100">
    </el-table-column>
    <el-table-column
      prop="u_money"
      label="账户余额(万元)"
      width="100">
    </el-table-column>
    <el-table-column
      prop="u_status"
      label="用户状态"
      width="100">
    </el-table-column>
    <el-table-column
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-row>
          <el-button @click="reacharage(scope.row)" type="success">充值</el-button>
        </el-row>
      </template>
    </el-table-column>
  </el-table>
  <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 5, 10, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</div>
</template>


<script>
export default {
  data(){
    return {
        admins:[],
        currentsize:1,
        pagesize:3,
        // 分页
        currentPage: 1,
        total:0,
        pagesize:3,
    }
  },
  methods:{
    init(){
      let obj = {
        currentsize:this.currentPage, 
        pagesize:this.pagesize
      }
      this.$ajax({
        url:"after/user",
        method:"get",
        params:obj
      }).then(resp=>{
        if(resp.data.status === 1){
          let admin = resp.data.message
          this.total = resp.data.totalPage
          admin.forEach(item=>{
            item.u_status=1?item.u_status="正常":item.u_status="禁用"
          })
          this.admins = admin;
        }
      })
      
    },
    // 数据表格
    tableRowClassName({row, rowIndex}) {
      if (rowIndex%2 === 1) {
        return 'warning-row';
      } else{
        return 'success-row';
      }
    },
    // 分页
    handleSizeChange(val) {
      this.pagesize = val
      this.init();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.init();
      // console.log(`当前页: ${val}`);
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    goBack() {
      this.$router.push("/")
    },
    // 充值
    reacharage(row){
      // console.log(row.u_id);
      this.$prompt('请输入充值金额(万元)', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[1-9]\d*(\.\d+)?$/,
        inputErrorMessage: '充值不能为负数'
      }).then(({ value }) => {
        
        // this.number = value
        // 进货
        this.$ajax({
          url:"after/user",
          method:"put",
          data:{id:row.u_id, money:value}
        }).then(resp=>{
          if(resp.data.status == 1){
            this.$message({
              type: 'success',
              message: '你的充值了: ' + value + '万元'
            });
            this.init();
          }else{
            this.$message({
              type: 'info',
              message: '充值失败'
            }); 
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消充值'
        });       
      });
    }
  },
  created(){
    this.init();
  }
}
</script>

<style scoped>
/* 数据表格 */
.el-table .warning-row {
    background: oldlace;
  }
.el-table .success-row {
  background: #f0f9eb;
}
</style>